package com.egoo.ticket.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egoo.ticket.server.dao.MsgSettingRelationMapper;
import com.egoo.ticket.server.dao.MsgTemplateMapper;
import com.egoo.ticket.server.dao.SysDictDao;
import com.egoo.ticket.server.pojo.entity.MsgSettingRelation;
import com.egoo.ticket.server.pojo.entity.MsgTemplate;
import com.egoo.ticket.server.pojo.vo.MsgSettingRelationVo;
import com.egoo.ticket.server.pojo.vo.MsgTemplateVo;
import com.egoo.ticket.server.pojo.vo.SysDictVo;
import com.egoo.ticket.server.service.IMsgSettingRelationService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author donny
 * @since 2020-10-29
 */
@Service
public class MsgSettingRelationServiceImpl extends ServiceImpl<MsgSettingRelationMapper, MsgSettingRelation> implements IMsgSettingRelationService {

	@Autowired
	SysDictDao sysDictDao;
	@Autowired
	MsgTemplateMapper msgTemplateMapper;

	/**
	 * @Author: Donny_dong
	 * @Description: 获取设置条件的数据源
	 * @Param:
	 * @return:
	 * @Date: 2020/11/19
	 */
	@Override
	public MsgSettingRelationVo getData(String userId, String tenantId) {
		List<SysDictVo> msgTargetList = sysDictDao.selectListByDictCode(ConstantCode.SysDictCode.MSG_NOTIFY_TARGET, tenantId);
		List<SysDictVo> msgTypeList = sysDictDao.selectListByDictCode(ConstantCode.SysDictCode.MSG_TYPE, tenantId);
		QueryWrapper<MsgTemplate> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(MsgTemplate::getStatus,ConstantCode.STATUS_NOMAL_OLD).eq(MsgTemplate::getTenantId,tenantId);
		List<MsgTemplate> msgTemplates = msgTemplateMapper.selectList(queryWrapper);

		for (SysDictVo s:msgTypeList
			 ) {
			if (CollectionUtils.isEmpty(msgTemplates)){
				break;
			}
			ArrayList<MsgTemplate> templateArrayList = new ArrayList<>();
			for (MsgTemplate m:msgTemplates
				 ) {
				if (s.getDictCode().equals(m.getTemplateType())){
					templateArrayList.add(m);
				}
			}
			s.setMsgTemplateList(templateArrayList);
		}
		MsgSettingRelationVo msgSettingRelationVo = new MsgSettingRelationVo();
		msgSettingRelationVo.setMsgTargetList(msgTargetList)
							.setMsgTypeList(msgTypeList);
		return msgSettingRelationVo;
	}
}
